//
//  NewestViewController.m
//  Project_B
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "NewestViewController.h"
#import "ZYBIgCollectionCell.h"
#import "Twenty-fourViewController.h"
#import "ZYImageViewController.h"
#import "ZYSpecialController.h"
#import "ZYDetailWebViewController.h"
@interface NewestViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate,BigCollectionDeleage>

@property(nonatomic,strong)UIScrollView *smallScrollView;/**< 新闻 小ScrollView*/
@property(nonatomic,strong)UICollectionView *myCollectionView;/**< 新闻 大CollectionView*/


@property (nonatomic, retain) UIView * vernier;/**< 游标*/
@property (nonatomic, assign) CGFloat currenX;/**<当前坐标*/
@property (nonatomic, assign) BOOL isRight;/**<监听手势方向,默认为YES*/
@property (nonatomic, retain) NSMutableArray *btnArray;/**<按钮数组*/
@property (nonatomic, assign) NSInteger currentIndex;/**<记录状态*/

@property (nonatomic, retain) NSArray *titleArray;/**< 新闻 标题数组*/
@property (nonatomic, retain) NSArray *urlArray;/**< 新闻 接口数组*/
@end

@implementation NewestViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //关闭navigationBar 透明
    self.navigationController.navigationBar.translucent = NO;
   
    
    //创建 小ScrollView
    [self makeSmallScrollView];
    
    //创建 大CollectionView
    [self makeCollectionView];
    
    
    self.isRight = NO;
    self.currentIndex = 0;
    self.btnArray = [NSMutableArray array];
    
    
    CGFloat w = SCREEN_WIDTH/6;
   
    
    self.vernier = [[UIView alloc]init];
    self.vernier.backgroundColor = [UIColor colorWithHue:0.02 saturation:1 brightness:0.91 alpha:1];
    [self.view addSubview:self.vernier];
    
    [_vernier mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(SCREEN_WIDTH/26);
        make.top.mas_equalTo(SCREEN_WIDTH/11 - 3);
        make.width.mas_equalTo(w - SCREEN_WIDTH/13);
        make.height.mas_equalTo(3);
        
    }];
    self.vernier.center = CGPointMake(w / 2, 3);

    [self.smallScrollView addSubview:self.vernier];
    
    
    
    self.titleArray = [NSArray arrayWithObjects:@"头条",@"娱乐",@"科技",@"体育",@"财经",@"军事",@"哒哒",@"原创",@"游戏",@"轻松",@"健康",@"数码", nil];
    [self setTitleContent:_titleArray];

    self.urlArray = [NSArray arrayWithObjects:
                     @"T1348647853363",
                     @"T1348648517839",
                     @"T1348649580692",
                     @"T1348649079062",
                     @"T1348648756099",
                     @"T1348648141035",
                     @"T1444289532601",
                     @"T1370583240249",
                     @"T1348654151579",
                     @"T1350383429665",
                     @"T1414389941036",
                     @"T1348649776727",nil];
    
    
    
    
#pragma mark  添加24小时按钮

    //设置左边按钮图片
    UIImage *leftImage = [UIImage imageNamed:@"top_navi_bell_normal@2x.png"];
    //取消渲染
    leftImage = [leftImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnAction)];

    
#pragma mark  网易图标
    UIImageView *wangYiView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45*ADAPT_WIDTH, 22*ADAPT_HEIGHT)];
    wangYiView.image = [UIImage imageNamed:@"navbar_netease@2x.png"];
    self.navigationItem.titleView = wangYiView;

    
#pragma mark - 添加搜索按钮
    
    //设置右边按钮图片
    UIImage *rightImage = [UIImage imageNamed:@"search_icon@2x.png"];
    //取消渲染
    rightImage = [rightImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    
}

#pragma mark - 24按钮方法
-(void)leftBtnAction{
    
    Twenty_fourViewController *VC = [[Twenty_fourViewController alloc]init];
   
    VC.navigationController.navigationBar.hidden = YES;
    [self.navigationController pushViewController:VC animated:YES];
 
}
   
#pragma mark - 搜索按钮方法
- (void)searchAction
{
    LHSearchController *search = [[LHSearchController alloc] init];
    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:search];
    
    na.navigationBar.barTintColor = [UIColor colorWithHue:0 saturation:0.74 brightness:0.91 alpha:1];
   
    [self presentViewController:na animated:YES completion:^{
        
        
    }];
}




#pragma mark - 创建小标题按钮
- (void)setTitleContent:(NSArray *)array{
    
    CGFloat w = SCREEN_WIDTH / 6;
    
    NSInteger i = 0;
    
    for (NSString *title in array) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.smallScrollView addSubview:btn];
        
        btn.frame = CGRectMake(0, 0, w - SCREEN_WIDTH/15, SCREEN_WIDTH/11 - 3);
        
        btn.center = CGPointMake(w * i + w/2 , (SCREEN_WIDTH/11 - 3)/2);
        btn.titleLabel.font = [UIFont systemFontOfSize:ADAPT_SYSTEM_FONT];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHue:0.58 saturation:0.01 brightness:0.61 alpha:1] forState:UIControlStateNormal];
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self.btnArray addObject:btn];
        i++;
    }
    
    self.smallScrollView.contentSize = CGSizeMake(w * array.count + w, 0);
}



#pragma mark - 按钮方法
- (void)btnAction:(UIButton *)btn{
    
    CGFloat w = SCREEN_WIDTH;
    self.myCollectionView.contentOffset = CGPointMake(w * (btn.tag - 100), 0);
    
    NSLog(@"===%f",self.myCollectionView.contentOffset.x);
    
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect temp = self.vernier.frame;
        temp.origin.x = SCREEN_WIDTH/26 + w / 6 * (btn.tag - 100);
        self.vernier.frame = temp;
        
    } completion:^(BOOL finished) {
        
    }];
   

   
}



#pragma mark - 拖动时触发方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (self.currenX > scrollView.contentOffset.x) {
        self.isRight = NO;
    }else{
        self.isRight = YES;
    }
    
    [UIView animateWithDuration:0.2 animations:^{
        
        CGRect temp = self.vernier.frame;
        CGFloat index = scrollView.contentOffset.x / SCREEN_WIDTH;
        temp.origin.x = SCREEN_WIDTH/26 + SCREEN_WIDTH / 6 * index;
        self.vernier.frame = temp;
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
            CGPoint point = CGPointMake(0, 0);
            
            if (self.currentIndex != index) {
                self.currentIndex = index;
//                NSLog(@"%ld", self.currentIndex);
            }
            
            if (index >= 5 && index < self.btnArray.count - 5) {
                point.x = SCREEN_WIDTH / 6 * index ;
                self.smallScrollView.contentOffset = point ;
            }else if (index < 5){
                self.smallScrollView.contentOffset = point;
            }
            
            if (self.smallScrollView.contentOffset.x <= 0) {
                point.x = 0;
                self.smallScrollView.contentOffset = point;
            }else if (self.smallScrollView.contentOffset.x >= self.smallScrollView.contentSize.width){
                point.x = self.smallScrollView.contentSize.width;
                self.smallScrollView.contentOffset = point;
            }
            
        }];
        
    }];
    
    self.currenX = scrollView.contentOffset.x;
}




#pragma mark - 拖动结束时触发方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.currenX = self.vernier.frame.origin.x;
    

}





#pragma mark - smallScrollView
-(void)makeSmallScrollView{
    
    self.smallScrollView = [[UIScrollView alloc]init];
    self.smallScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.smallScrollView];
    self.smallScrollView.showsHorizontalScrollIndicator = NO;
    self.smallScrollView.bounces = NO;
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    [_smallScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
//        make.width.mas_equalTo(SCREEN_WIDTH);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(SCREEN_WIDTH/11);
        
    }];

    
}


#pragma mark - 大CollectionView
-(void)makeCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置最小间距
    flowLayout.minimumLineSpacing = 0;
    
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - (SCREEN_WIDTH/11 + self.tabBarController.tabBar.frame.size.height + self.navigationController.navigationBar.frame.size.height + 20));
    //设置水平滑动
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    
    //设置按页滑动
    self.myCollectionView.pagingEnabled = YES;
    
    [_myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.top.equalTo(self.smallScrollView.mas_bottom);
//        make.width.mas_equalTo(SCREEN_WIDTH);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(SCREEN_HEIGHT - (SCREEN_WIDTH/11 + self.tabBarController.tabBar.frame.size.height + self.navigationController.navigationBar.frame.size.height + 20));
       
    }];
    
    
    //注册
    [self.myCollectionView registerClass:[ZYBIgCollectionCell class] forCellWithReuseIdentifier:@"ZYBIgCollectionCell"];
    
}


#pragma mark - 设置item数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titleArray.count;
}


#pragma mark - 设置cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZYBIgCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYBIgCollectionCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    
    cell.urlTidStr = [self.urlArray objectAtIndex:indexPath.row];
    
    //设置代理人
    cell.bigCollectionDeleage = self;
    
    
    
    return cell;
}

#pragma mark -  轮播图跳转 协议方法
-(void)presentUrlToController:(NSString *)url :(NSNumber *)replyCount{
    
    if (url.length > 15) {
        
        ZYDetailWebViewController *webViewVC = [[ZYDetailWebViewController alloc]init];
        webViewVC.docId= url;
        webViewVC.replyCount = replyCount;
        [self.navigationController pushViewController:webViewVC animated:YES];
        
    }else{
        
        
        ZYImageViewController *imageVC = [[ZYImageViewController alloc]init];
        
        
        imageVC.url = url;
        imageVC.replyStr = replyCount;
        imageVC.navigationController.navigationBar.hidden = YES;
        [self.navigationController pushViewController:imageVC animated:YES];
        
    }
    
    
}


#pragma mark - BigCollectionCell 协议方法
-(void)presentDataToController:(NewestModel *)model{
    
    
    if ([model.skipType isEqualToString:@"special"]) {
        
        ZYSpecialController *specialVC = [[ZYSpecialController alloc]init];
        specialVC.specialId = model.specialID;
        specialVC.navBarTitle = model.title;
        [self.navigationController pushViewController:specialVC animated:YES];

       
    }else if ([model.skipType isEqualToString:@"photoset"]){
        
        ZYImageViewController *imageVC = [[ZYImageViewController alloc]init];
        
        imageVC.url = model.photosetID;
        imageVC.replyStr = model.replyCount;

        [self.navigationController pushViewController:imageVC animated:YES];
        
    

    }else{
        ZYDetailWebViewController *webViewVC = [[ZYDetailWebViewController alloc]init];
        webViewVC.docId= model.docid;
        webViewVC.replyCount = model.replyCount;
        [self.navigationController pushViewController:webViewVC animated:YES];

    }

    
    
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
